#ifndef STRATEGY_PARSER_H
#define STRATEGY_PARSER_H

#include "GeometryTypes.h"
#include <regex>
#include <algorithm>

class StrategyParser {
public:
    AnalysisResult parseStrategy(const Strategy& strategy) {
        AnalysisResult result;
        std::string text = strategy.strategy;
        
        // 转换为小写便于匹配
        std::transform(text.begin(), text.end(), text.begin(), ::tolower);
        
        // 分类操作类型
        result.operationType = classifyOperation(text, strategy.elementIdentifier);
        
        // 提取参数
        result.parameters = extractParameters(text, strategy.elementIdentifier, result.operationType);
        
        // 确定计算方法
        result.calculationMethod = determineCalculationMethod(result.operationType, result.parameters);
        
        // 确定需要的前置元素
        result.requiredElements = getRequiredElements(result.operationType, result.parameters);
        
        // 设置执行顺序
        result.executionOrder = calculateExecutionPriority(result.operationType);
        
        return result;
    }

private:
    std::string classifyOperation(const std::string& text, const std::vector<std::string>& elements) {
        if (std::regex_search(text, std::regex("放置在坐标系原点|放置在.*位置"))) {
            return "point_placement";
        } else if (std::regex_search(text, std::regex("以.*为圆心|半径.*绘制"))) {
            return "circle_creation";
        } else if (std::regex_search(text, std::regex("过.*作.*切线|切线"))) {
            return "tangent_construction";
        } else if (std::regex_search(text, std::regex("连接.*形成线段"))) {
            return "line_connection";
        } else if (std::regex_search(text, std::regex("对称|相交"))) {
            return "relation_verification";
        } else if (std::regex_search(text, std::regex("垂直于"))) {
            return "perpendicular_construction";
        }
        return "unknown";
    }
    
    // 提取参数 
    // text 是策略文本，"将点O放置在坐标系原点(0,0)，根据OC=4将点C放置在(4,0)位置"
    // elements 是 elementIdentifier
    std::map<std::string, std::any> extractParameters(const std::string& text, 
                                                    const std::vector<std::string>& elements,
                                                    const std::string& opType) {
        std::map<std::string, std::any> params;
        params["elements"] = elements;
        
        if (opType == "point_placement") {
            // 修复：提取具体的坐标位置
            std::regex coordRegex(R"(\((\d+),(\d+)\))");
            std::smatch match;
            if (std::regex_search(text, match, coordRegex)) {
                double x = std::stod(match[1].str());
                double y = std::stod(match[2].str());
                params["position"] = Point(x, y);
            } else {
                params["position"] = Point(0, 0);
            }
            
            // 提取距离约束 OC=4
            std::regex distanceRegex(R"(([A-Z]{2})=(\d+))");
            std::smatch distanceMatch;
            if (std::regex_search(text, distanceMatch, distanceRegex)) {
                std::map<std::string, std::any> distanceConstraint;
                distanceConstraint["segment"] = distanceMatch[1].str();
                distanceConstraint["length"] = std::stod(distanceMatch[2].str());
                params["distance_constraint"] = distanceConstraint;
            }
        } else if (opType == "circle_creation") {
            std::regex radiusRegex(R"(半径(\d+))");
            std::smatch match;
            if (std::regex_search(text, match, radiusRegex)) {
                params["radius"] = std::stod(match[1].str());
            } else {
                // 默认半径
                params["radius"] = 5.0;
            }
        } else if (opType == "tangent_construction") {
            std::regex perpendicularRegex(R"(垂直于([A-Z]+))");
            std::smatch match;
            if (std::regex_search(text, match, perpendicularRegex)) {
                params["perpendicular_to"] = match[1].str();
            }
        }
        
        return params;
    }
    
    // 在 StrategyParser.h 中修复方法映射逻辑
    std::string determineCalculationMethod(const std::string& opType, 
                                        const std::map<std::string, std::any>& params) {
        if (opType == "point_placement") {
            if (params.find("distance_constraint") != params.end()) {
                return "place_points_by_distance";
            }
            return "place_points_absolute";
        } else if (opType == "circle_creation") {
            return "draw_circle_by_center_radius";
        } else if (opType == "tangent_construction") {
            if (params.find("perpendicular_to") != params.end()) {
                return "construct_perpendicular_tangent";
            }
            return "construct_tangent_at_point";  // 确保这个方法存在
        } else if (opType == "line_connection") {
            return "connect_points";
        } else if (opType == "relation_verification") {
            return "verify_symmetry";
        } else if (opType == "perpendicular_construction") {
            return "construct_perpendicular_tangent";
        }
        return "unknown_method";
    }
    
    std::vector<std::string> getRequiredElements(const std::string& opType, 
                                                const std::map<std::string, std::any>& params) {
        auto elements = std::any_cast<std::vector<std::string>>(params.at("elements"));
        
        if (opType == "point_placement") {
            return {};
        } else if (opType == "circle_creation") {
            return {elements[0]}; // 第一个元素是圆心
        } else if (opType == "tangent_construction") {
            std::vector<std::string> required = {"C"};
            if (params.find("perpendicular_to") != params.end()) {
                auto perpendicularTo = std::any_cast<std::string>(params.at("perpendicular_to"));
                required.push_back(perpendicularTo);
            }
            return required;
        }
        return elements;
    }
    
    int calculateExecutionPriority(const std::string& opType) {
        std::map<std::string, int> priorityMap = {
            {"point_placement", 1},
            {"line_connection", 2},
            {"circle_creation", 3},
            {"perpendicular_construction", 4},
            {"tangent_construction", 5},
            {"relation_verification", 6},
            {"unknown", 999}
        };
        return priorityMap[opType];
    }
};

#endif